﻿var isMobile = $(window).width() <= 768;

$(function () {
    // This command is used to initialize some elements and make them work properly
    $.material.init();

    $("img.lazy").lazyload({
        effect: "fadeIn",
        threshold: 200,
        failure_limit: 3,
        data_attribute: 'src',
        placeholder: '/Content/images/lazyplaceholder.png'
    });

    if (isMobile) {
        $('#navbar').addClass('navbar-fixed-top');
    } else {
        $(window).scroll(calcFixedNav);
        $(window).resize(calcFixedNav);
    }
});

function getCurrentScroll() {
    return window.pageYOffset || document.documentElement.scrollTop;
}

function calcFixedNav() {
    var nav = $('#navbar');

    if (isMobile) {
        nav.addClass('navbar-fixed-top');
        return;
    }

    var scroll = getCurrentScroll();
    var underNav = $('#undernav');
    var underNavHeight = underNav.is(':visible') ? underNav.height() : nav.height();
    if (scroll >= nav.offset().top && scroll >= underNavHeight) {
        nav.addClass('navbar-fixed-top');
        if (!isMobile) {
            $('#navbar-menu .navbar-brand').show();
        }
    }
    else {
        nav.removeClass('navbar-fixed-top');
        if (!isMobile) {
            $('#navbar-menu .navbar-brand').hide();
        }
    }
}